package com.swallow.weixin.utils;

import com.swallow.weixin.entity.vo.ResultVO;
import com.swallow.weixin.enums.GlobalEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;

public class ResponseBuilderUtil {
    private static final Logger logger = LoggerFactory.getLogger(ResponseBuilderUtil.class);

    /**
     * 构建成功返回的json
     *
     * @return 返回构建对象
     */
    public static ResultVO buildSuccessJson() {
        return buildSuccessJson(null);
    }

    /**
     * 构建成功返回的json 携带参数
     *
     * @param data 参数
     * @return 返回构建对象
     */
    public static ResultVO buildSuccessJson(Object data) {
        logger.debug("[ResponseBuilderUtil][buildSuccessJson] entering data:{}", data);
        ResultVO resultVO = new ResultVO();
        resultVO.setResult(ResultVO.RESULT_SUCCESS);
        if (data != null) {
            resultVO.setData(data);
        }
        logger.info("[ResponseBuilderUtil][buildSuccessJson] 构建成功返回的json 携带参数 data:{},resultVO:{}", data, resultVO);
        return resultVO;
    }

    /**
     * 构建错误json
     * 构建
     *
     * @return
     */
    public static ResultVO buildErrorJSON() {
        return buildGlobalJSON(GlobalEnum.SYSTEM_ERROR, "", ResultVO.RESULT_FAILED,null);
    }

    /**
     * 构建错误json
     *
     * @return
     */
    public static ResultVO buildErrorJSON(GlobalEnum globalEnum) {
        return buildGlobalJSON(globalEnum, "", ResultVO.RESULT_FAILED,null);
    }

    /**
     * 构建错误json
     *
     * @return
     */
    public static ResultVO buildErrorJSON(GlobalEnum globalEnum, String message) {
        return buildGlobalJSON(globalEnum, message, ResultVO.RESULT_FAILED,null);
    }

    /**
     * 构建正确访问的json
     *
     * @param globalEnum
     * @return
     */
    public static ResultVO buildSuccessJson(GlobalEnum globalEnum) {
        return buildSuccessJson(globalEnum, null);
    }

    /**
     * 构建正确访问的json
     *
     * @param globalEnum
     * @return
     */
    public static ResultVO buildSuccessJson(GlobalEnum globalEnum, Object data) {
        return buildGlobalJSON(globalEnum, "", ResultVO.RESULT_SUCCESS, data);
    }

    /**
     * 构建返回json
     *
     * @return
     */
    public static ResultVO buildGlobalJSON(GlobalEnum globalEnum, String message, int result, Object data) {
        logger.debug("[ResponseBuilderUtil][buildGlobalJSON] entering globalEnum:{},message:{},result:{},data:{}", globalEnum, message, result, data);
        ResultVO resultVO = new ResultVO();
        resultVO.setResult(result);
        if (result == ResultVO.RESULT_FAILED) {
            resultVO.setErrorCode(globalEnum.getCode());
        }
        if (StringUtils.isNotBlank(message)) {
            resultVO.setMessage(message);
        } else {
            resultVO.setMessage(globalEnum.getMessage());
        }
        if(data!=null){
            resultVO.setData(data);
        }
        logger.info("[ResponseBuilderUtil][buildGlobalJSON] desc globalEnum:{},message:{},resultVO:{},result:{},data:{}", globalEnum, message, resultVO, result, data);
        return resultVO;
    }

    /**
     * 构建返回 jsonView返回的 modelAdminView
     *
     * @param resultVO 封装对象
     * @return 返回json对应的modelAdminView对象
     */
    public static ModelAndView buildJsonView(ResultVO resultVO) {
        ModelAndView modelAndView = new ModelAndView("jsonview");
        modelAndView.addObject("resultVO", resultVO);
        return modelAndView;
    }
}
